<?php
include_once(PATH_SQL . 'upload_image_product_sql.php');
include_once(PATH_MODEL . 'upload_image_product_model.php');
include_once(PATH_VIEW_ADMIN . 'upload_image_product_view.php');
include_once(PATH_INCLUDE."thumb.php");
include_once(PATH_INCLUDE."constant.php");

$oModelupload_image_product=new Model_upload_image_product();
$oViewupload_image_product = new View_upload_image_product($oModelupload_image_product);

$oModelupload_image_product->setData('page_size',50);
$oModelupload_image_product->setData('pagegroup_size',5);
$oModelupload_image_product->setData('stylesheet',"paging");

$sAction = $_REQUEST['sAction'];
$oUpload = new Upload($_FILES);


$sAction=$_REQUEST['sAction'];

switch($sAction)
{
	case '':
		{
			$oModelupload_image_product->setData('product_id',$_REQUEST['pictures_product_id']);
			$oViewupload_image_product->displayManagerIndex($oModelupload_image_product);
			$oViewupload_image_product->listImageOfThisP($oModelupload_image_product,$_REQUEST['pictures_product_id']);
			$oViewupload_image_product->displayListImageUploaded($oModelupload_image_product);
			break;
		}
	case "deleteImageList": 
		{
			$oViewupload_image_product->displayManagerIndex($oModelupload_image_product);
			$oModelupload_image_product->setData('pictures_id',$_REQUEST['pictures_id']);
			$oModelupload_image_product->setData('product_id',$_REQUEST['product_id']);
			$oModelupload_image_product->setData('pictures_file_name',$_REQUEST['pictures_file_name']);
			$oViewupload_image_product->resultXML_deleteImageList($oModelupload_image_product,$_REQUEST['pictures_id'],$_REQUEST['product_id'],$_REQUEST['pictures_file_name']);
			die();
			break;		
		}
	case "upDateListImage": 
		{	
			$oViewupload_image_product->displayManagerIndex($oModelupload_image_product);
			$oModelupload_image_product->setData('pictures_id',$_REQUEST['pictures_id']);
			$oModelupload_image_product->setData('product_id',$_REQUEST['product_id']);
			$oViewupload_image_product->resultXML_AddImageProduct($oModelupload_image_product,$_REQUEST['pictures_id'],$_REQUEST['product_id']);
			die();
			break;		
		}			
	case 'initialize_edit':
		{
			$oModelupload_image_product->setData('pictures_product_id',$_REQUEST['pictures_product_id']);
			$oModelupload_image_product->setData('product_id',$_GET['pictures_product_id']);
			$oViewupload_image_product->displayManagerIndex($oModelupload_image_product);
			$oViewupload_image_product->listImageOfThisP($oModelupload_image_product,$_GET['pictures_product_id']);
			$oViewupload_image_product->displayListImageUploaded($oModelupload_image_product);
			break;
		}	
	case "filter_image": 
		{
			$oViewupload_image_product->displayManagerIndex($oModelupload_image_product);
			$oModelupload_image_product->setData('pictures_file_name',$_REQUEST['pictures_file_name']);
			$oModelupload_image_product->setData('pictures_title',$_REQUEST['pictures_title']);
			$oModelupload_image_product->setData('pictures_note',$_REQUEST['pictures_note']);
			
			$aData = array("pictures_file_name"=>$_REQUEST["pictures_file_name"],
							"pictures_title"=>$_REQUEST["pictures_title"],
							"pictures_note"=>$_REQUEST["pictures_note"],
							"and_or"=>$_REQUEST["and_or"],);
			$oViewupload_image_product->resultXML_filter_image($oModelupload_image_product,$aData);
			die();
			break;		
		}
	case "add_new_form":
		{
			$oModelupload_image_product->setData("do_edit_or_add_new","do_add_new");
			$oViewupload_image_product->displayAddNewForm($oModelupload_image_product);
			$oViewupload_image_product->viewListProvince($oModelupload_image_product);
			break;
		}
	case "exe_upload_image":{	
		$img_id=$oModelupload_image_product->GenPicsId();
		$_REQUEST["pictures_date_uploaded"]  = date('Y-m-d H:i:s');
		$oModelupload_image_product->setData("pictures_date_uploaded",$_REQUEST["pictures_date_uploaded"]);
		$oModelupload_image_product->setData("pictures_product_id",'');
		$oModelupload_image_product->setData("pictures_id",$img_id);
		$check_insert_image_table = $oModelupload_image_product->insertImage();

            if ($check_insert_image_table==0) 
			{
				
                //upload image process
            if ($oUpload->getFileName("pictures_file_name") != "") 	{
				if ($oUpload->isAllowImageFile("pictures_file_name")) {
                        $ext = $oUpload->getFileExtension("pictures_file_name");
                        $fileUpload = $img_id . "." . $ext;
                        if ($oUpload->saveAs($fileUpload, PATH_UPLOAD_PRODUCT 	,"pictures_file_name", "over", 0777)) 
						{
								
						$thumb=new thumbnail_1(PATH_UPLOAD_PRODUCT.$fileUpload);	// generate image_file, set filename to resize
						/**
						 * Set thumb 300x200
						 */
						$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH_300);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT_200);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO_300);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PRODUCT."thumb300x200/".$fileUpload);
						/**
						 * Set thumb 160x120
						 */
						$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PRODUCT."thumbnails/".$fileUpload);
			
						
						$oModelupload_image_product->setData("pictures_id",$img_id);
						$oModelupload_image_product->setData("pictures_file_name",$fileUpload);
						$aData = array("pictures_id"=>$img_id,"pictures_file_name"=>$fileUpload);
						$oModelupload_image_product->update_file_name($aData);
                       	redir('?sMod=upload_image_product&pictures_product_id='.$_REQUEST["product_id_upload"].'&sAction=initialize_edit');
 
                        }
                        else 
						{
							redir('?sMod=upload_image_product&sStatus=uploadImageError');
                       		//$aUpadteImage['product_category_id'] = $check_insert;
                            //$aUpadteImage['pictures_file_name'] = $fileUpload;
                            //$oModelupload_image_product->updateCategoryIcon($aUpadteImage);
                        }
                    }
                }
                
            }
            else
        	{
        		$sErrorMsg =  $oModelupload_image_product->aLanguage['ALERT']['form_cannot_submit'] . $sErrorMsg;
				$oModelupload_image_product->setErrorMsg($sErrorMsg);
        	}
			break;
		}

}

$oViewupload_image_product->displayPage($oModelupload_image_product);
?>